<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 查询快速
 *
 * @author      Eden
 * @datetime    2016/12/2 22:47
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Util_AliExpress
{
    private $getAppKey = '';
    const URL = 'http://ali-deliver.showapi.com';
    const COM = '/showapi_expInfo';

    private $error = '';

    public function __construct()
    {
        $this->getAppKey = $this->_getKey();
    }

    public function _getKey()
    {
        $ci = & get_instance();
        $query = $ci->db->get_where('setting', array(
            'use' => 'system',
            'key' => 'service'
        ));
        if ($res = $query->row_array()) {
            try {
                $res = json_decode($res['value'], true);
                return !empty($res['aliExpress']) ? value($res, 'aliExpress') : false;
            } catch(Exception $e) {}
        }
        return false;
    }

    /**
     * 获取错误
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 获取所有快递公司
     * @return mixed
     */
    public function getComp()
    {
        $url = self::URL .'/showapi_expressList?maxSize=1000';
        $content = $this->request($url);

        try {
            return json_decode($content, true);
        } catch (PDOException $e) {}
    }

    /**
     * 获取消息
     * @param string $com
     * @param string $nu
     * @return bool
     */
    public function ask($com = '', $nu = '')
    {
        if (empty($com) || empty($nu)) {
            $this->error = '缺少参数';
            return false;
        }

        $querys = array(
            'com' => $com,
            'nu' => $nu
        );

        $url = self::URL . self::COM . "?" . http_build_query($querys);
        $content = $this->request($url);
        try {
            return json_decode($content, true);
        } catch (PDOException $e) {}
    }

    protected function request($url, $params = array(), $method = 'GET', array $multipart = array(), array $extra_headers = array())
    {
        $this->error = null;
        $this->data = null;

        $method = strtoupper($method);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $headers = (array)$extra_headers;
        array_push($headers, "Authorization:APPCODE " . $this->getAppKey);
        switch ($method) {
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, true);
                if (!empty($params)) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params);
                }
                break;
            case 'DELETE':
            case 'GET':
                if ($method == 'DELETE') {
                    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                }
                if (!empty($params)) {
                    $url = $url . (strpos($url, '?') !== false ? '&' : '?')
                        . (is_array($params) ? http_build_query($params) : $params);
                }
                break;
        }

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_URL, $url);

        $response = curl_exec($curl);
        $this->httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $this->httpInfo = curl_getinfo($curl) ?: array();
        curl_close($curl);

        return $response;
    }


}
